﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Practices.ObjectBuilder;
using Microsoft.Practices.CompositeWeb;
using DataContracts;
using System.ServiceModel;

namespace TrainingWorkshop.MVPWebsite.User.Views
{
    public class UserListPresenter : Presenter<IUserListView>
    {

        // NOTE: Uncomment the following code if you want ObjectBuilder to inject the module controller
        //       The code will not work in the Shell module, as a module controller is not created by default
        //
        // private IUserController _controller;
        // public UserListPresenter([CreateNew] IUserController controller)
        // {
        // 		_controller = controller;
        // }

        private IUserController _usercontroller;
        public UserListPresenter([CreateNew] IUserController usercontroller)
        {
            _usercontroller = usercontroller;
        }


        public override void OnViewLoaded()
        {
            // TODO: Implement code that will be executed every time the view loads
        }

        public override void OnViewInitialized()
        {
            // TODO: Implement code that will be executed the first time the view loads
            View.Users = _usercontroller.GetAllUser();
            View.Departments = _usercontroller.GetAllDepartment();
        }

        // TODO: Handle other view events and set state in the view


        public void OnDeleteUser(int userId)
        {
            try
            {
                _usercontroller.DeleteUser(userId);
                
                View.SkipToAnotherPage("UserList.aspx");

            }
            catch (FaultException ex)
            {
                View.ShowOperationMessage(ex.Message);
                View.SkipToAnotherPage("UserList.aspx");

            }
            
        }

        public void OnCreateUser()
        {
            View.SkipToAnotherPage("CreateNewUser.aspx");
 
        }

        public void OnEditUser()
        {
            View.SkipToAnotherPage("CreateNewUser.aspx");
        }



        public void OnSearchUser(int departmentId, string firstName, string lastName)
        {


            if (View.FirstName == "" && View.LastName == "" && View.DepartmentId == 0)
            {
                View.Users = _usercontroller.GetAllUser();
                View.Departments = _usercontroller.GetAllDepartment();
            }

            else
            {
                List<UserDTO> list = _usercontroller.SearchUser(departmentId, firstName, lastName);
                if (list.Count == 0)
                {
                    View.Users = null;
                    View.ShowOperationMessage("Sorry!Do not have matching User.");
                }
                else
                {
                    View.Users = list;
                }

            }
        }

    }

}





